<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/8/17
 * Time: 14:26
 */

namespace app\admin\controller;

use app\common\controller\Common;
use think\Db;
use think\Validate;

/**
 * Class GoodsGroup
 * @package app\admin\controller 商品分类
 */
class GoodsGroup extends Common
{
    public function index()
    {
        $num = Db::name('GoodsGroup')
            ->count();
        $pageObject = new \Page($num);
        $list = Db::name('GoodsGroup')
            ->select();
        $list = menu_left($list, 'id', 'pid');
        $group_list = Db::name('GoodsGroup')
            ->select();
        $group_list = menu_left($group_list, 'id', 'pid');
        $map = [
            'num' => $num,
            'list' => $list,
            'pages' => $pageObject->showPages(3),
            'group_list' => $group_list
        ];

        return view('index', $map);
    }

    public function add()
    {
        $validate = new Validate([
            ['name', 'require|unique:menu', '分类名称必填|菜单名称重复'],
        ]);
        if (!$validate->check($_POST)) {
            $this->error($validate->getError());
        }
        $add_data = $_POST;
        $add_data['create_time'] = time();
        if ($id = Db::name('goodsGroup')->insertGetId($add_data)) {
            $save_menu['id'] = $id;
            $save_menu['nav_list'] = get_goods_group_nav($id, input('pid'));
            Db::name('goodsGroup')->update($save_menu);

            /* rm_cache_menu();*/
            $this->success('分类添加成功');
        } else {
            $this->success('分类添加失败');
        }
    }

    /**
     * 删除分类
     */
    public function del()
    {
        $childMenu = Db::name('goodsGroup')->where('pid', input('id'))->select();
        if (!empty($childMenu)) {
            $this->error('当前分类下存在子子分类,请谨慎操作');
        } else {
            if (Db::name('goodsGroup')->delete(input('id'))) {
                $this->success('当前分类删除成功', url('admin/GoodsGroup/index'));
            } else {
                $this->error('当前分类删除失败');
            }
        }

    }

    /**
     * 分类编辑
     */
    public function edit()
    {
        if (!$_POST) {
            $id = input('id');
            $list = Db::name('GoodsGroup')
                ->where(array('id' => $id))
                ->find();

            $group = Db::name('GoodsGroup')
                ->select();
            $group = menu_left($group, 'id', 'pid');
            $map = array(
                'list' => $list,
                'group' => $group
            );
            return view('edit', $map);
        } else {
            $validate = new Validate([
                ['name', 'require|unique:menu', '分类名称必填|菜单名称重复'],
            ]);
            if (!$validate->check($_POST)) {
                $this->error($validate->getError());
            }
            $add_data = $_POST;
            $add_data['update_time'] = time();
            if ($id = Db::name('goodsGroup')->update($add_data)) {
                $save_menu['id'] = input('id');
                $save_menu['nav_list'] = get_goods_group_nav($id, input('pid'));
                Db::name('goodsGroup')->update($save_menu);

                /* rm_cache_menu();*/
                $this->success('分类添加成功');
            } else {
                $this->success('分类添加失败');
            }
        }

    }
}